package com.Iconcat.system.controller;

import com.Iconcat.system.entity.SystemSetting;
import com.Iconcat.system.response.Result;
import com.Iconcat.system.service.SystemSettingsService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/system_setting")
@CrossOrigin
@Api(value = "系统设置模块",tags = "系统设置接口")
public class SystemSettingController {
    @Resource
    private SystemSettingsService systemSettingService;

    @GetMapping("/{id}")
    public Result getSystemSetting(@PathVariable Integer id) {
        SystemSetting systemSetting = systemSettingService.getById(id);
        if (systemSetting != null) {
            return Result.ok().data("systemSetting", systemSetting);
        } else {
            return Result.error().message("System setting not found");
        }
    }

    @PostMapping
    public Result createSystemSetting(@RequestBody SystemSetting systemSetting) {
        boolean success = systemSettingService.save(systemSetting);
        if (success) {
            return Result.ok().message("System setting created successfully");
        } else {
            return Result.error().message("Failed to create system setting");
        }
    }

    @PutMapping("/{id}")
    public Result updateSystemSetting(@PathVariable Integer id, @RequestBody SystemSetting systemSetting) {
        systemSetting.setId(id);
        boolean success = systemSettingService.updateById(systemSetting);
        if (success) {
            return Result.ok().message("System setting updated successfully");
        } else {
            return Result.error().message("Failed to update system setting");
        }
    }

    @DeleteMapping("/{id}")
    public Result deleteSystemSetting(@PathVariable Integer id) {
        boolean success = systemSettingService.removeById(id);
        if (success) {
            return Result.ok().message("System setting deleted successfully");
        } else {
            return Result.error().message("Failed to delete system setting");
        }
    }

}
